import { rsadecryptData, attestationCode } from "../common/js/rsa";
import { SUCCESS_STATUS } from "../common/js/constant";
import { aesDecrypt } from "../common/js/crypto";
import { msg } from "../common/js/util";

export default function passData(res) {
  const {
    responseHead: { responseCode, responseMsg, random = "", sign = "" },
    responseBody = {},
  } = res;
  if (responseCode === SUCCESS_STATUS) {
    // 请求成功的情况
    // 拼接签名
    let bsign = `responseCode=${responseCode}responseMsg=${responseMsg}${responseBody}`;
    // 解密返回的数据, data就是最终解密后的数据
    let data = aesDecrypt(responseBody, rsadecryptData(random))
      ? JSON.parse(aesDecrypt(responseBody, rsadecryptData(random)))
      : {};
    // 验证签名
    if (attestationCode(bsign, sign)) {
      // 验证通过
      return data;
    }
    return {responseCode};
  } else {
    console.log("responseCode", responseCode);
    // 请求失败
    msg(responseCode + ":" + responseMsg);
    if (responseCode == '100013') {
      uni.navigateTo({
        url: `/pages/auth/login`,
      });
    }
  }
  return null;
}
